var express = require('express');
var router = express.Router();
const md5 = require('blueimp-md5')
const models = require('../db/models')
//const UserModel = models.getModel('user')
const _filter = {'pwd': 0, '__v': 0} // 查询时过滤掉
const sms_util = require('../util/sms_util')
const users = {}
const ajax = require('../api/ajax')
var svgCaptcha = require('svg-captcha')
var pool = require("../util/pool");

/*
获取商家列表
*/
router.get('/stores',function(req,res){
  var sql ="SELECT  * FROM store";
  pool.query(sql,(err,result)=>{
    if(err)throw err;
    //console.log(result);
    if(result.length===0){
      res.send({code:-1,msg:"没有获取到商家列表"})
    }else{ 
     return res.send({code: 1, msg: '获取成功',data:{result}})
    }
  })
})
/*
获取某个商家
*/
router.post('/store',function(req,res){
  //获取商家sid
  let sid = req.body.sid;
  console.log(sid);
  var sql ="SELECT  * FROM store WHERE sid=?";
  pool.query(sql,[sid],(err,result)=>{
    if(err)throw err;
    console.log(result);
    if(result.length===0){
      res.send({code:-1,msg:"没有获取到该商家"})
    }else{ 
     return res.send({code: 1, msg: '获取成功',data:{result}})
    }
  })
})
/*
获取某商家商品类别信息
*/
router.post('/category',function(req,res){
  //获取商家sid
  let sid = req.body.sid;
  console.log(sid);
  var sql ="SELECT  * FROM category WHERE store_id=?";
  pool.query(sql,[sid],(err,result)=>{
    if(err)throw err;
    console.log(result);
    if(result.length===0){
      res.send({code:-1,msg:"没有获取到该商家商品类别信息"})
    }else{ 
     return res.send({code: 1, msg: '获取成功',data:{result}})
    }
  })
})
/*
获取某商家商品信息
*/
router.post('/foods',function(req,res){
  //获取商家sid
  let sid = req.body.sid;
  console.log(sid);
  var sql ="SELECT  * FROM goods WHERE store_id=?";
  pool.query(sql,[sid],(err,result)=>{
    if(err)throw err;
    console.log(result);
    if(result.length===0){
      res.send({code:-1,msg:"没有获取到该商家商品信息"})
    }else{ 
     return res.send({code: 1, msg: '获取成功',data:{result}})
    }
  })
})
/*
密码登陆
 */
router.post('/login_pwd', function (req, res) {
  const name = req.body.name2
  const pwd = req.body.pwd2
  //const captcha = req.body.captcha.toLowerCase()
  //输出用户输入参数是否正确  captcha 你输入验证码字符串
  console.log('/login_pwd', name, pwd);
  //服务器中保存验证码    二个相同
  //console.log("server_", req.session)

  // 可以对用户名/密码格式进行检查, 如果非法, 返回提示信息
  // if(captcha!==req.session.captcha) {
  //   return res.send({code: -1, msg: '验证码不正确'})
  // }
  // 删除保存的验证码
  // delete req.session.captcha

   var sql = "SELECT  uid FROM user WHERE uname = ? AND upwd = ?";
   pool.query(sql,[name,pwd],(err,result)=>{
    if(err)throw err;
    if(result.length===0){
      res.send({code:-1,msg:"用户名或密码有误"})
    }else{ 
     var id = result[0].uid;
      //登录成功在服务器端将登录凭证保存
      req.session.userid = id;
     return res.send({code: 1, msg: '登录成功',data:{id}})
    }
   })

})
/*
注册
*/
router.post('/register', function (req, res) {
  const name = req.body.name1
  const pwd = req.body.pwd1
  const tel = req.body.tel
  var sql = 'INSERT user(uname,upwd,phone) VALUES(?,?,?)';
  pool.query(sql,[name,pwd,tel],(err,result)=>{
    if(err)throw err;
    res.send({msg: '注册成功', code: 1})
  })
})
/*
充值
*/
router.post('/chongzhi',function(req,res) {
  const uid = req.session.userid
  const money = req.body.money1 + req.body.money2
  var sql = "UPDATE user SET money=? WHERE uid = ?";
  pool.query(sql,[money,uid],(err,result)=>{
    if(err)throw err;
    res.send({msg:'充值成功',code:1})
  })
})
/*
结算
*/
router.post('/jiesuan',function(req,res) {
  const uid = req.session.userid
  const money = req.body.yu
  var sql = "UPDATE user SET money=? WHERE uid = ?";
  pool.query(sql,[money,uid],(err,result)=>{
    if(err)throw err;
    res.send({msg:'结算成功',code:1})
  })
})
/*
创建订单
*/
router.post('/create_order',function(req,res) {
  const uid = req.session.userid
  const fname = req.body.fname
  const fprice = req.body.fprice
  const fcount = req.body.fcount
  var sql = 'INSERT orders(fname,fprice,fcount,user_id) VALUES(?,?,?,?)';
  pool.query(sql,[fname,fprice,fcount,uid],(err,result)=>{
    if(err)throw err;
    res.send({msg:'创建订单成功',code:1})
  })
})
/*
获取订单列表
*/
router.get('/get_orders',function(req,res){
  const uid = req.session.userid
  var sql ="SELECT  * FROM orders WHERE user_id=?";
  pool.query(sql,[uid],(err,result)=>{
    if(err)throw err;
    console.log(result);
    if(result.length===0){
      res.send({code:-1,msg:"没有获取到订单"})
    }else{ 
     return res.send({code: 1, msg: '获取订单成功',data:{result}})
    }
  })
})
/*
一次性图形验证码
 */
// router.get('/captcha', function (req, res) {
//   var captcha = svgCaptcha.create({
//     ignoreChars: '0o1l',
//     noise: 2,
//     color: true
//   });
//   req.session.captcha = captcha.text.toLowerCase();
//   console.log(2,req.session.captcha);
//   res.type('svg');
//   res.send(captcha.data)
// });



/*
根据sesion中的userid, 查询对应的user
 */
router.get('/userinfo', function (req, res) {
  let uid = req.session.userid;
  console.log(uid);
  var sql = "SELECT  * FROM user WHERE uid = ?";
  pool.query(sql,[uid],(err,result)=>{
    if(err)throw err;
    if(result.length===0){
      res.send({code:-1,msg:"没有查到此数据"})
    }else{ 
     console.log(result);
     return res.send({code: 1, msg: '查找用户成功',data:{result}})
    }
   })

})


router.get('/logout', function (req, res) {
  // 清除浏览器保存的userid的cookie
  delete req.session.userid
  // 返回数据
  res.send({code: 0})
})


module.exports = router;